import { defineStore } from "pinia"
import axios from "axios"
import type { articlesData} from "@/modules/manage";
import type {responseArticleData} from "@/modules/users";

export const useArticleStore = defineStore("articleDetail", {
    persist: true,
    state: () => ({
        articleContent:''
    }),
    actions: {
        async getArticlesById(id:number){
            const response = await axios.get(`http://192.168.1.8:8000/articles/user/${id}`);
            const middleData = response.data.data;
            const transData:articlesData[] = middleData.map((article: responseArticleData)=>{
                return {
                    id: article.articleId,
                    title: article.title,
                    content: article.content,
                    tags: article.tags,
                    authorName: article.username,
                };
            })
            return transData;
        },
        async getArticlesByTag(id:number){
            const response = await axios.get(`http://192.168.1.8:8000/articles/tag/${id}`);
            const middleData = response.data.data;
            const transData:articlesData[] = middleData.map((article: responseArticleData)=>{
                return {
                    id: article.articleId,
                    title: article.title,
                    content: article.content,
                    tags: article.tags,
                    authorName: article.username,
                };
            })
            return transData;
        }
    }
})